<template>
  <div class="table-container">
    <div class="table-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>一键报价</el-breadcrumb-item>
        <el-breadcrumb-item>报价列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="table-search">
      卖场：
      <el-select
        v-model="marketId"
        clearable
        @change="selectMarket"
        filterable
        placeholder="请选择卖场"
      >
        <el-option
          v-for="item in marketList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <el-row style="margin-left:10px;">
        <el-input
          style="width: 200px;margin-right: 10px"
          placeholder="请输入报价信息查询"
          v-model="keyword"
          clearable
          @clear="resetData"
        >
        </el-input>
        <el-button type="primary" @click="resetData" >搜索</el-button>
      </el-row>
    </div>
    <div class="table-content">
    <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="待报价" name="1"></el-tab-pane>
        <el-tab-pane label="已报价" name="2"></el-tab-pane>
        <el-tab-pane label="已取消" name="0"></el-tab-pane>
    </el-tabs>
      <el-table
        border
        empty-text="当前数据空空如也"
        ref="multipleTable"
        :data="offerList"
        tooltip-effect="dark"
        style="width: 100%"
        @select="selectedRow"
        @select-all="selectedAll"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="ID" width="60"></el-table-column>
        <el-table-column prop="room_type" label="房屋类型" width="80"></el-table-column>
        <el-table-column prop="area" label="面积" width="80"></el-table-column>
        <el-table-column prop="username" label="用户名" width="auto"></el-table-column>
        <el-table-column prop="mobile" label="手机号" width="auto"></el-table-column>
        <!-- <el-table-column prop="parlour_num" label="客厅数" width="auto"></el-table-column>
        <el-table-column prop="bedroom_num" label="卧室数" width="auto"></el-table-column>
        <el-table-column prop="kitchen_num" label="厨房数" width="auto"></el-table-column>
        <el-table-column prop="balcony_num" label="阳台数" width="auto"></el-table-column> -->
        <el-table-column prop="create_time" label="创建时间" width="auto" :formatter="changeTime"></el-table-column>
        <el-table-column prop="market_name" label="所属卖场" width="auto"></el-table-column>
        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button
              v-if="activeName=='1'"
              size="mini"
              style="margin-right: 5px"
              @click="handleOffer(scope.$index, scope.row)">确认报价
            </el-button>
            <el-button v-if="activeName=='1'" @click="toCanael(scope.$index, scope.row)" type="warning" size="mini">取消</el-button>
            <el-button slot="reference" @click="delOffer(scope.$index, scope.row)" type="danger" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-nav">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 50,100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        page: 1,
        pageSize: 10,
        total: 0,
        keyword: '',
        offerList: [],
        multipleSelection: [],
        activeName:'1',
        marketList:[], //卖场列表
        marketId:'', //已选卖场id
      }
    },
    mounted() {
      this.queryList();
      this.remoteMethod()
    },
    methods: {
        // 获取卖场列表
        remoteMethod() {
          this.$http.post("/api/root/Market/marketList").then((res) => {
            console.log(res, "卖场列表");
            if (res.code == 1) {
              this.marketList = res.data ? res.data : [];
            }
          });
        },
        // 选择卖场
        selectMarket() {
          this.page = 1
          this.pageSize = 10
          this.queryList();
        },

        changeTime(row, column, cellValue, index){
            if(cellValue){
                return this.$moment(cellValue*1000).format('yyyy-MM-DD HH:ss')
            }
        },

        //   删除按钮
        delOffer(index,row){
            console.log(index,row,'1111');
            this.$confirm('此操作将删除该报价, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(() => {
                this.toDelete(row.id)
            })
        },

        handleBatchDel() {
            if (this.multipleSelection.length < 1) {
                this.$message.error("请先勾选需要删除的优惠券！");
                return;
            }
            this.$confirm('确定要执行批量删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.toDelete(this.multipleSelection.join(","))
            }).catch(() => {
                console.log('放弃删除')
            });

        },
        selectedRow(selection, row) {
            this.multipleSelection = [];
            const _ths = this;
            selection.map(function (item) {
                _ths.multipleSelection.push(item.id);
            })
        },
        selectedAll(rows) {
            if (rows.length === 0) {
                this.multipleSelection = [];
            } else {
                const _ths = this;
                rows.map(function (item) {
                    if (!_ths.multipleSelection.includes(item.id)) {
                    _ths.multipleSelection.push(item.id);
                    }
                })
            }
        },

        // 确认报价
        handleOffer(index,row){
            this.$confirm('确认完成该报价吗？','提示',{
                confirmButtonText:'确定',
                cancelButtonText:'取消',
                type:'warning'
            }).then(()=>{
                this.$http.post('/api/root/OnekeyOffer/changeStatus',{status:2,id:row.id}).then(res=>{
                    if(res.code==1){
                        this.$message.success('操作成功')
                        this.queryList()
                    }else{
                        this.$message.warning(res.msg)
                    }
                })
            })
        },

        // 取消报价
        toCanael(index,row){
            this.$confirm('确认取消该报价吗？','提示',{
                confirmButtonText:'确定',
                cancelButtonText:'取消',
                type:'warning'
            }).then(()=>{
                this.$http.post('/api/root/OnekeyOffer/changeStatus',{status:0,id:row.id}).then(res=>{
                    if(res.code==1){
                        this.$message.success('操作成功')
                        this.queryList()
                    }else{
                        this.$message.warning(res.msg)
                    }
                })
            })
        },

        handleSizeChange(val) {
            this.pageSize = val;
            this.page = 1;
            this.queryList()
        },
        handleCurrentChange(val) {
            this.page = val;
            this.queryList();
        },

        // 切换tab
        handleClick(){
            this.page = 1
            this.pageSize = 10
            this.queryList()
        },
        queryList() {
            this.$http.get('/api/root/OnekeyOffer/index', {
                page: this.page,
                pagesize: this.pageSize,
                keyword: this.keyword,
                status:this.activeName,
                market_id:this.marketId
            }).then(res => {
                if (res.code == 1) {
                    this.offerList = res.data.list?res.data.list:[];
                    this.total = res.data.total;
                }
            })
        },
        toDelete(id_lst) {
            this.$http.post('/api/root/OnekeyOffer/del', {
                id: id_lst,
            }).then(rest => {
                if (rest.code == 1) {
                    this.queryList();
                    this.multipleSelection = [];
                    this.$message.success('删除成功');
                } else {
                    this.$message.error("删除失败");
                }
            })
        }
        ,
        resetData(){
            this.page = 1;
            this.queryList();
        }
    }
  }
</script>
<style lang="less" scoped>
  @import 'list.less';
</style>
